package com.le.tester.leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * createTime：2024/4/16 9:38
 * description：寻找两个正序数组的中位数
 */
public class MedianSortedArrays {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {

        int num1Length = nums1.length;
        int num2Length = nums2.length;
        int b = num1Length + num2Length;


        List<Integer> numList = new ArrayList<>();

        for (int value : nums1) {
            numList.add(value);
        }

        for (int val : nums2) {
            numList.add(val);
        }

        numList = numList.stream().sorted().collect(Collectors.toList());

        if (numList.size() % 2 == 0) {
            double e1 = Double.valueOf(numList.get(b / 2));
            double e2 = Double.valueOf(numList.get(b / 2 - 1));
            return (e1 + e2) / 2;
        } else {
            return Double.valueOf(numList.get(b / 2));
        }

    }

    public static void main(String[] args) {
        int[] ints = {1, 3};
        int[] ints1 = {2};
        System.out.println(new MedianSortedArrays().findMedianSortedArrays(ints, ints1));

    }
}
